{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Testing a P5 Javascript magic in a Python kernel:"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%p5\n",
"\n",
" var x = 100; \n",
" var y = 100;\n",
"\n",
" function setup () {\n",
" createCanvas(700, 410);\n",
" };\n",
"\n",
" function draw () {\n",
" background(0);\n",
" fill(255);\n",
" rect(x,y,50,50);\n",
" };"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%p5\n",
"\n",
" var x = 100; \n",
" var y = 100;\n",
"\n",
" function setup () {\n",
" createCanvas(600, 400);\n",
" };\n",
"\n",
" function draw () {\n",
" background(0);\n",
" fill(255);\n",
" rect(x,y,150,150);\n",
" };"
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 102,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%p5 \n",
"\n",
"var capture;\n",
"\n",
"function setup() {\n",
" createCanvas(390, 240);\n",
" capture = createCapture(VIDEO);\n",
" capture.size(320, 240);\n",
" //capture.hide();\n",
"}\n",
"\n",
"function draw() {\n",
" background(255);\n",
" image(capture, 0, 0, 320, 240);\n",
" filter('INVERT');\n",
"}\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The magic:"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from IPython.core.magic import register_cell_magic\n",
"from IPython.display import IFrame\n",
"\n",
"TEMPLATE = \"\"\"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"Script: %(name)s
\n",
"\n",
"\n",
"\n",
"\"\"\"\n",
"\n",
"COUNT = 0\n",
"\n",
"@register_cell_magic\n",
"def p5(line, cell):\n",
" global COUNT\n",
" filename = \"p5-%s.html\" % COUNT\n",
" COUNT += 1\n",
" with open(filename, \"w\") as fp:\n",
" fp.write(TEMPLATE % {\"script\": cell, \"name\": filename})\n",
" return IFrame(filename, width=\"100%\", height=\"400\")\n",
"\n",
"del p5"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}